package com.ksr.logistics.controller;

import com.github.pagehelper.PageInfo;
import com.ksr.logistics.pojo.*;
import com.ksr.logistics.service.PermissionService;
import com.ksr.logistics.service.RoleService;
import com.ksr.logistics.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/list")
    @RequiresPermissions("role:list")
    @ResponseBody
    public PageInfo<Role> show(Integer pageNum,Integer pageSize,String keyword){
        List<Role> roles=roleService.selPageRoles(pageNum,pageSize,keyword);
        PageInfo<Role> infos=new PageInfo<>(roles);
        if (infos.getSize()==0 && infos.getPageNum()>1){
            roles=roleService.selPageRoles(pageNum-1,pageSize,keyword);
            infos=new PageInfo<>(roles);
        }
        for (Role role:roles) {
            List<User> users=userService.selByRoleID(role.getRoleId().intValue());
            if (users.size()>0){
                StringBuilder sb=new StringBuilder();
                for (User u:users) {
                    sb.append(u.getRealname()+"，");
                }
                sb.deleteCharAt(sb.length()-1);
                role.setUsersInfo(sb.toString());
            }
            List<Permission> permissions=permissionService.selIn(role.getPermissionIds());
            if (permissions.size()>0){
                StringBuilder sb=new StringBuilder();
                for (Permission p:permissions) {
                    sb.append(p.getName()+"，");
                }
                sb.deleteCharAt(sb.length()-1);
                role.setPermissionsInfo(sb.toString());
            }

        }
        return infos;
    }

    @RequestMapping("/delete")
    @RequiresPermissions("role:delete")
    @ResponseBody
    public Result delete(Integer roleId){
        List<User> users=userService.selByRoleID(roleId);
        Result result=new Result();
        result.setCode(0);
        result.setMsg("删除失败,请重试");
        if (users.size()==0){
            int res= roleService.deleteById(roleId);
            if (1==res){
                result.setCode(1);
                result.setMsg("删除成功!");
            }
        }else{
            result.setMsg("该角色拥有很多用户,暂时无法删除");
        }
        return result;
    }

    @RequestMapping("/deletes")
    @RequiresPermissions("role:deletes")
    @ResponseBody
    public Result deletes(@RequestParam("ids[]") List<Integer> ids){
        Result result=new Result();
        result.setCode(0);
        result.setMsg("选中角色删除失败,请重试");
        int rows=0;
        for (Integer id:ids) {
            if (1==delete(id).getCode()){
                rows++;
            }
        }
        if (rows>=1){
            result.setCode(1);
            if (rows==ids.size()){
                result.setMsg("成功删除所有选中角色!");
            }else{
                result.setMsg("成功删除角色"+rows+"个<br>部分选中角色拥有很多用户,暂时无法删除!");
            }
        }
        return result;
    }

    @RequestMapping("/edit")
    @RequiresPermissions("role:edit")
    public String roleEdit(Model m,Integer roleId){
        if (null!=roleId){
            Role role=roleService.selById(roleId);
            m.addAttribute("role",role);
        }
       return "roleEdit";
    }

    @RequestMapping("/insert")
    @RequiresPermissions("role:insert")
    @ResponseBody
    public Result insert(Role role){
        Result result=new Result(0,"新增角色失败,请重试!");
        int row=roleService.insertRole(role);
        if (1==row){
            result.setCode(1);
            result.setMsg("新增角色成功!");
        }
        return result;
    }

    @RequestMapping("/checkRoleName")
    @RequiresPermissions("role:checkRoleName")
    @ResponseBody
    public boolean checkRoleName(String rolename,Integer roleId){
        boolean res=true;
        if (null!=roleId){
            Role r=roleService.selById(roleId);
            if (rolename.equals(r.getRolename())){
                return true;
            }
        }
        List<Role> list=roleService.selByRoleName(rolename);
        if (list.size()>0){
            res=false;
        }
        return res;
    }

    @RequestMapping("/update")
    @RequiresPermissions("role:update")
    @ResponseBody
    public Result update(Role r){
        Result result=new Result(0,"角色信息修改失败,请重试");
        int row=roleService.updateRole(r);
        if (1==row){
            result.setCode(1);
            result.setMsg("角色信息修改成功!");
        }
        return result;
    }
}
